/*
███████╗██╗   ██╗ ██████╗██╗  ██╗
██╔════╝██║   ██║██╔════╝██║ ██╔╝
█████╗  ██║   ██║██║     █████╔╝ 
██╔══╝  ██║   ██║██║     ██╔═██╗ 
██║     ╚██████╔╝╚██████╗██║  ██╗
╚═╝      ╚═════╝  ╚═════╝╚═╝  ╚═╝
*/
package Service;

import dao.ProductDao;
import dao.ReviewDao;
import common.JavaBean.Product;
import common.utils.PageInfo;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductService {
    private ProductDao productDao = new ProductDao();
    private ReviewDao reviewDao = new ReviewDao();

    // 管理员功能
    public int addProduct(String name, BigDecimal price, String desc, String imgUrl) {
        try {
            return productDao.addProduct(name, price, desc, imgUrl);
        } catch (SQLException e) {
            throw new RuntimeException("添加商品失败", e);
        }
    }

    public boolean updateProduct(int productId, String newName, BigDecimal newPrice,
                                 String newDesc, String newImgUrl, Integer newStatus) {
        try {
            productDao.updateProduct(productId, newName, newPrice, newDesc, newImgUrl, newStatus);
            return true;
        } catch (SQLException e) {
            throw new RuntimeException("更新商品失败", e);
        }
    }

    // 用户功能
    public List<Product> getProducts(Integer productId, String keyword,
                                     BigDecimal minPrice, BigDecimal maxPrice, Integer status) {
        try {
            List<Product> products;
            try (ResultSet rs = productDao.queryProducts(productId, keyword, minPrice, maxPrice, status)) {
                products = new ArrayList<>();
                while (rs.next()) {
                    Product p = new Product();
                    p.setProductId(rs.getInt("product_id"));
                    p.setName(rs.getString("name"));
                    p.setPrice(rs.getBigDecimal("price"));
                    p.setDescription(rs.getString("description"));
                    p.setImageUrl(rs.getString("image_url"));
                    p.setStatus(rs.getInt("status"));
                    products.add(p);
                }
            }
            return products;
        } catch (SQLException e) {
            throw new RuntimeException("查询商品失败", e);
        }
    }

    public Product getProductWithReviews(int productId) {
        try {
            List<Product> products = getProducts(productId, null, null, null, 1);
            if (!products.isEmpty()) {
                return products.get(0);
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException("获取商品失败", e);
        }
    }

    public Integer getProductbyname(String productname) {
        try {
            Integer ret=null;
            try (ResultSet res = productDao.askProducts(null, productname, null, null, null))
            {
                while (res.next()) {
                    ret=res.getInt("product_id");
                }
            }
            return  ret;
        } catch (Exception e) {
            throw new RuntimeException("获取商品失败", e);
        }
    }
}